﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.移库管理
{
    /// <summary>
    /// 移库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class TransferBinOrderController : BaseController
    {
        private readonly ITransferBinOrderService transferBinOrderService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="transferBinOrderService"></param>
        public TransferBinOrderController(IWebHostEnvironment environment, ITransferBinOrderService transferBinOrderService) : base(environment)
        {
            this.transferBinOrderService = transferBinOrderService;
        }

        /// <summary>
        /// 获取移库列表显示
        /// </summary>
        /// <param name="key"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTransferBinOrderList(string? key, int status, int pageIndex=1, int pageSize=20)
        {

            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = transferBinOrderService.GetTransferBinOrderList(key, status,pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加移库单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTransferBinOrder(TransferBinOrder m)
        {
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            string message = "";
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool IsOK = transferBinOrderService.AddTransferBinOrder(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 删除移库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTransferBinOrder(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = transferBinOrderService.DelTransferBinOrder(id, out string message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！"+message, "500"));
        }
        /// <summary>
        /// 批量删除移库单
        /// </summary>
        /// <param name="ridList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTransferBinOrderList(long[] ridList)
        {
            if (ridList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = true;
            string message="";
            foreach (var rid in ridList)
            {
                bool isOK = transferBinOrderService.DelTransferBinOrder(Convert.ToInt32(rid), out message);
                if (!isOK)
                {
                    IsOK = false;
                    break;
                }
            }
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！"+message, "500"));
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTransferBinOrderById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var mobj = transferBinOrderService.GetTransferBinOrderById(id);
            if (mobj != null)
            {
                return Ok(BaseResult.Result(mobj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 修改移库单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTransferBinOrder(TransferBinOrder m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            bool IsOK = transferBinOrderService.UpdTransferBinOrder(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }
    }
}
